﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PitOfDespair.Extensions
{
    public static class RandomExtensions
    {
        /// <summary>
        /// Next inclusive
        /// </summary>
        /// <param name="rnd"></param>
        /// <returns></returns>
        public static int NextI(this Random rnd, int minValue, int maxValue)
        {
            return rnd.Next(minValue, maxValue + 1);
        }
        /// <summary>
        /// Next inclusive
        /// </summary>
        /// <param name="rnd"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public static int NextI(this Random rnd, int maxValue)
        {
            return rnd.Next(maxValue + 1);
        }
        public static int NextSign(this Random rnd)
        {
            return rnd.NextI(1) > 0 ? 1 : -1;
        }
        public static bool NextBool(this Random rnd)
        {
            return rnd.NextI(0, 1) > 0;
        }

        public static T GetRandom<T>(this IEnumerable<T> source, Random rnd)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            var count = source.Count();
            if (count < 1)
                throw new ArgumentOutOfRangeException("source");
            var index = rnd.Next(0, count - 1);
            return source.ElementAt(index);
        }
    }
}
